package com.xxl.job.executor.service.jobhandler;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.executor.core.entity.AddAiCheckEventCountMQ;
import com.xxl.job.executor.core.entity.AddAiCheckEventMQ;
import com.xxl.job.executor.core.entity.AddAiEventImageMQ;
import com.xxl.job.executor.service.utils.ApiUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.activation.MimetypesFileTypeMap;
import java.io.File;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * XxlJob开发示例（Bean模式）
 * <p>
 * 开发步骤：
 * 1、在Spring Bean实例中，开发Job方法，方式格式要求为 "public ReturnT<String> execute(String param)"
 * 2、为Job方法添加注解 "@XxlJob(value="自定义jobhandler名称", init = "JobHandler初始化方法", destroy = "JobHandler销毁方法")"，注解value值对应的是调度中心新建任务的JobHandler属性的值。
 * 3、执行日志：需要通过 "XxlJobLogger.log" 打印执行日志；
 *
 * @author xuxueli 2019-12-11 21:52:51
 */
@Component
public class RabbitMQXxlJob {

    @Value( "${xxl.job.rabbitMQ.ip}" )
    private String IP;
    @Value( "${xxl.job.rabbitMQ.port}" )
    private int PORT;
    @Value( "${xxl.job.rabbitMQ.user}" )
    private String USER;
    @Value( "${xxl.job.rabbitMQ.pwd}" )
    private String PWD;
    @Value( "${xxl.job.USERCODE}" )
    private String USERCODE;
    @Value( "${xxl.job.STATIONCODE}" )
    private String STATIONCODE;

    private final static String STATIONNAME = "内乡县非现场执法点";

    /**
     * 1、AI事件数据上传
     *
     * @param param
     * @return
     * @throws Exception
     */
    @XxlJob( "sjlwAiEventData" )
    public ReturnT<String> sjlwAiEventData(String param) throws Exception {
        String[] paramArr = param.split(";");
        String USERCODE = paramArr[0];
        String STATIONCODE = paramArr[1];
        String STATIONNAME = paramArr[2];
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(IP);
        factory.setPort(PORT);
        factory.setUsername(USER);
        factory.setPassword(PWD);
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        String LaneNo = "1";
        String dateTime = new SimpleDateFormat("yyMMddHHmmssSSSS").format(new Date());
        AddAiCheckEventMQ addAiCheckEventMQ = new AddAiCheckEventMQ();
        addAiCheckEventMQ.setCheckNo(USERCODE + LaneNo + dateTime);
        addAiCheckEventMQ.setPlate("豫AT9U69");
        addAiCheckEventMQ.setVehicleModelId("J10");
        addAiCheckEventMQ.setVehicleModelName("轿车");
        addAiCheckEventMQ.setPlateColor("蓝");
        addAiCheckEventMQ.setPlateTypeId(4);
        addAiCheckEventMQ.setPlateType("小型汽车");
        addAiCheckEventMQ.setBodyColor("白");
        addAiCheckEventMQ.setEventTypeId(0);
        addAiCheckEventMQ.setEventTypeName("正常");
        addAiCheckEventMQ.setDirection("上行");
        addAiCheckEventMQ.setLaneNo(Integer.valueOf(LaneNo));
        addAiCheckEventMQ.setSpeed(35);
        addAiCheckEventMQ.setCheckTime(new SimpleDateFormat("yyyyMMddHHmmssSSSS").format(new Date()));
        addAiCheckEventMQ.setStationId(USERCODE);
        addAiCheckEventMQ.setStationName(STATIONNAME);
        addAiCheckEventMQ.setStationCode(STATIONCODE);
        String message = JSON.toJSONString(addAiCheckEventMQ);
        String QUEUE_NAME = "sjlw_ai_event_data";
        // 声明队列
        channel.queueDeclare(QUEUE_NAME, true, false, false, null);
        // 发行消息到队列
        String exchange = "sjlw.ai.exchange";
        String routingKey = "sjlw_ai_event_data";
        channel.basicPublish(exchange, routingKey, true, null, message.getBytes());
        XxlJobLogger.log("args = [" + message + "]");
        channel.close();
        connection.close();
        XxlJobLogger.log("XXL-JOB, AI事件数据上传.");
        return ReturnT.SUCCESS;
    }


    /**
     * 2、AI事件图片上传
     *
     * @param param
     * @return
     * @throws Exception
     */
    @XxlJob( "sjlwAiEventImage" )
    public ReturnT<String> sjlwAiEventImage(String param) throws Exception {
        String[] paramArr = param.split(";");
        String USERCODE = paramArr[0];
        String STATIONCODE = paramArr[1];
        String STATIONNAME = paramArr[2];
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(IP);
        connectionFactory.setPort(PORT);
        connectionFactory.setUsername(USER);
        connectionFactory.setPassword(PWD);
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        String LaneNo = "1";
        String dateTime = new SimpleDateFormat("yyMMddHHmmssSSSS").format(new Date());
        File file = new File("D:\\123456.jpg");
        String base64Img = ApiUtils.ConvertImageToBase64(file);
        AddAiEventImageMQ addAiEventImageMQ = new AddAiEventImageMQ();
        addAiEventImageMQ.setCheckNo(USERCODE + LaneNo + dateTime);
        addAiEventImageMQ.setImgType("1");  //图片标识 1：车头 2：车身 3：车牌 4: 车头2  5: 车尾
        addAiEventImageMQ.setFileType(new MimetypesFileTypeMap().getContentType(file));
        addAiEventImageMQ.setImgBase64(base64Img);
        String message = JSON.toJSONString(addAiEventImageMQ);
        String QUEUE_NAME = "sjlw_ai_event_img";
        //声明队列
        channel.queueDeclare(QUEUE_NAME, true, false, false, null);
        //设置Exchange信息
        String exchange = "sjlw.ai.exchange";
        //设置routingKey信息
        String routingKey = "sjlw_ai_event_img";
        //发送信息
        //channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        channel.basicPublish(exchange, routingKey, true, null, message.getBytes());
        XxlJobLogger.log("args = [" + message + "]");
        channel.close();
        connection.close();
        XxlJobLogger.log("XXL-JOB, AI事件图片上传.");
        return ReturnT.SUCCESS;
    }

    /**
     * 3、统计信息上传
     *
     * @param param
     * @return
     * @throws Exception
     */
    @XxlJob( "sjlwAiStatistical" )
    public ReturnT<String> sjlwAiStatistical(String param) throws Exception {
        String[] paramArr = param.split(";");
        String USERCODE = paramArr[0];
        String STATIONCODE = paramArr[1];
        String STATIONNAME = paramArr[2];
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(IP);
        connectionFactory.setPort(PORT);
        connectionFactory.setUsername(USER);
        connectionFactory.setPassword(PWD);
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        String LaneNo = "1";
        AddAiCheckEventCountMQ addAiCheckEventCountMQ = new AddAiCheckEventCountMQ();
        addAiCheckEventCountMQ.setStartTime(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
        addAiCheckEventCountMQ.setEndTime(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
        addAiCheckEventCountMQ.setLaneNo(Integer.valueOf(LaneNo));
        addAiCheckEventCountMQ.setArrivalFlow(100);
        addAiCheckEventCountMQ.setLightVehicle(35);
        addAiCheckEventCountMQ.setHeavyVehicle(20);
        addAiCheckEventCountMQ.setAveSpeed(20);
        addAiCheckEventCountMQ.setTimeDistance(20);
        addAiCheckEventCountMQ.setSpaceDistance(1);
        addAiCheckEventCountMQ.setSpaceOccupyRation(new BigDecimal(18.35).setScale(2, BigDecimal.ROUND_HALF_UP));
        addAiCheckEventCountMQ.setTimeOccupyRation(new BigDecimal(18.35).setScale(2, BigDecimal.ROUND_HALF_UP));
        addAiCheckEventCountMQ.setQueueLength(35);
        addAiCheckEventCountMQ.setStationId(USERCODE);
        addAiCheckEventCountMQ.setStationName(STATIONNAME);
        addAiCheckEventCountMQ.setStationCode(STATIONCODE);
        String message = JSON.toJSONString(addAiCheckEventCountMQ);
        String QUEUE_NAME = "sjlw_ai_statistical";
        //声明队列
        channel.queueDeclare(QUEUE_NAME, true, false, false, null);
        //设置Exchange信息
        String exchange = "sjlw.ai.exchange";
        //设置routingKey信息
        String routingKey = "sjlw_ai_statistical";
        //发送信息
        channel.basicPublish(exchange, routingKey, true, null, message.getBytes());
        XxlJobLogger.log("args = [" + message + "]");
        channel.close();
        connection.close();
        XxlJobLogger.log("XXL-JOB,统计信息上传任务.");
        return ReturnT.SUCCESS;
    }

}
